package com.zxg.zbc.web.common.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.service.pipeline.PipelineContext;
import com.alibaba.citrus.service.pipeline.support.AbstractValve;
import com.alibaba.citrus.turbine.TurbineRunDataInternal;
import com.alibaba.citrus.turbine.util.TurbineUtil;

public class RedirctToHtml extends AbstractValve {
  final static Logger        log    = LoggerFactory.getLogger(DomianValve.class);
  @Autowired
  private HttpServletRequest request;
  @Autowired
  HttpServletResponse        response;

  private String             target = "index.html";

  @Override
  protected void init() throws Exception {

  }

  public void invoke(PipelineContext pipelineContext) throws Exception {
    TurbineRunDataInternal rundata = (TurbineRunDataInternal) TurbineUtil.getTurbineRunData(request);
    String url = request.getRequestURI();
    if (!url.equals("/")) {
      url = url.substring(1);
      url = url.substring(0, url.indexOf("/"));
      url = "/" + url + "/" + target;
    }

    String red = rundata.getRedirectLocation();
    log.warn(red);
    if (red == null) {
      if (url.startsWith("/admin")
//          || url.startsWith("/help")
//          || url.startsWith("/ad") || url.startsWith("/buyer") || url.startsWith("/seller")
//          || url.startsWith("/sellerh5") || url.startsWith("/dm") || url.startsWith("/auth") || url.startsWith("/news") || url.startsWith("/bd")
//          || url.startsWith("/data") || url.startsWith("/qr") || url.startsWith("/ds")
          ) {

        request.getRequestDispatcher(url).forward(request, response);
      }
    }
    pipelineContext.invokeNext();
  }

  public String getTarget() {
    return target;
  }

  public void setTarget(String target) {
    this.target = target;
  }

}
